﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using MySql.Data.MySqlClient;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionLib.Dao
{
    public class DaoPoint : DaoBase
    {

        public DaoPoint()
            : base()
        {
            this.TableName = "point";
            this.ViewName = "point";
        }

        public Point SelectById(Int32 OrionId)
        {
            lParam.Clear();
            Point retour = new Point();
            String req = this.DebutSelect
            + this.TableName
            + " WHERE OrionId=@orionid";

            lParam.Add(new MySqlParameter("@OrionId", OrionId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        public Point SelectByMouvementId(Int32 OrionMouvementLigneId)
        {
            lParam.Clear();
            Point retour = new Point();
            String req = this.DebutSelect
            + this.TableName
            + " WHERE OrionMouvementLigneId=@OrionMouvementLigneId";

            lParam.Add(new MySqlParameter("@OrionMouvementLigneId", OrionMouvementLigneId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        public List<Point> SelectAllActif()
        {
            lParam.Clear();
            List<Point> retour = new List<Point>();
            String req = this.DebutSelect
            + this.TableName
            + " WHERE OrionEtat=1";

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();

            return retour;
        }

        public List<Point> SelectAll()
        {
            lParam.Clear();
            List<Point> retour = new List<Point>();
            String req = this.DebutSelect
            + this.TableName;

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();

            return retour;
        }

        private Point ChargeFromReader(MySqlDataReader dr)
        {
            int i = 0;
            Point retour = new Point();
            retour.OrionId = dr.GetInt32("OrionId");

            i++;
            if (dr.IsDBNull(i))
                retour.Quantite = 0.0;
            else
                retour.Quantite = dr.GetDouble("Quantite");
            i++;
            if (dr.IsDBNull(i))
                retour.OrionMouvementLigneId = 0;
            else
                retour.OrionMouvementLigneId = dr.GetInt32("OrionMouvementLigneId");

            retour.OrionCrea = dr.GetDateTime("OrionCrea");
            retour.OrionMaj = dr.GetDateTime("OrionMaj");
            retour.OrionUserId = dr.GetInt32("OrionUserId");
            retour.OrionEtat = dr.GetBoolean("OrionEtat");

            return retour;
        }

        public Point Update(Point oe)
        {
            lParam.Clear();
            oe.OrionMaj = DateTime.Now;

            Point retour = new Point();
            String req = "UPDATE " + TableName + " SET Quantite=@Quantite,OrionMouvementLigneId=@OrionMouvementLigneId,OrionUserId=@OrionUserId, OrionMaj=@OrionMaj WHERE OrionId=@OrionId";

            lParam.Add(new MySqlParameter("@Quantite", oe.Quantite));
            lParam.Add(new MySqlParameter("@OrionMouvementLigneId", oe.OrionMouvementLigneId));

            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionMaj", oe.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionId", oe.OrionId));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(oe.OrionId);

            return retour;
        }

        public Point Insert(Point oe)
        {
            lParam.Clear();
            oe.OrionCrea = DateTime.Now;
            oe.OrionMaj = DateTime.Now;

            Point retour = new Point();
            String req = "INSERT INTO " + TableName + "(OrionId,Quantite,OrionMouvementLigneId,OrionEtat, OrionUserId, OrionCrea, OrionMaj)  VALUES(NULL,@Quantite,@OrionMouvementLigneId,@OrionEtat,@OrionUserId,@OrionCrea,@OrionMaj)";
            lParam.Add(new MySqlParameter("@Quantite", oe.Quantite));
            lParam.Add(new MySqlParameter("@OrionMouvementLigneId", oe.OrionMouvementLigneId));
            lParam.Add(new MySqlParameter("@OrionEtat", oe.OrionEtat));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionCrea", oe.OrionCrea));
            lParam.Add(new MySqlParameter("@OrionMaj", oe.OrionMaj));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(Convert.ToInt32(this.LastInsertId));

            return retour;
        }
    }
}